/*
 PMProfileManagementTableViewController.m
 Date Created: February 23, 2010
 
 Author: Frank Lau <flaua@sfu.ca>
 Author: Karol Krizka <kka34@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "PMProfileManagementTableViewController.h"

@implementation PMProfileManagementTableViewController
@synthesize profileList;

- (PMProfileManagementTableViewController *)initWithProfileList:(PMProfileList *)myprofileList {
	self = [super init];
	if( self ) {
		[self setProfileList:myprofileList];
	}

	return self;
}

//one section in table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

//number of rows in table equals the number of profiles in the list
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[profileList listAllProfiles] count]; 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	// Create a cell
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if(cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	
	PMProfile *profile=[[profileList listAllProfiles] objectAtIndex:indexPath.row];
	cell.textLabel.text = profile.profileName;

	return cell;
}

// Enable swipe-to-delete
- (void)tableView:(UITableView *)tableView 
        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
        forRowAtIndexPath:(NSIndexPath *)indexPath {
  if(editingStyle==UITableViewCellEditingStyleDelete) {
    PMProfile *profileToDelete=[[self.profileList listAllProfiles] objectAtIndex:indexPath.row];
    [self.profileList deleteProfile:profileToDelete.profileName];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                     withRowAnimation:UITableViewRowAnimationFade];
  }
}

@end
